'''
Author: 贵阳臻芯科技有限公司
Date: 2021-11-29 19:54:58
LastEditTime: 2021-12-01 00:05:50
LastEditors: Please set LastEditors
FilePath: \tcp_python\tcp_sever.py
Description: 
Copyright 2021 贵阳臻芯科技有限公司, All Rights Reserved. 
'''
import socket
import threading
import time 

def tcp_link(sock,addr):
    print('接收一个来自%s:%s连接请求'%addr)
    sock.send(b'welcome!')
    while True:
        data=sock.recv(1024)
        time.sleep(1)
        if not data or data.decode('utf-8')=='exit':
            break
        sock.send(('hello,%s!'% data.decode('utf-8')).encode('utf-8'))
    sock.close()
    print('来自%s:%s连接关闭了...'%addr)


s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(('127.0.0.1',8888))
s.listen(5)
print('等待客户端连接...')
while True:
    sock,addr=s.accept()
    t=threading.Thread(target=tcp_link,args=(sock,addr))
    t.start()
